#include "utils/timer_utils.hpp"
#include "behaviortree_cpp/bt_factory.h"
#include <thread>
#include <utils/timer_test.hpp>

// 模拟一些工作函数
void simulate_work(int milliseconds) {
    std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));//当前调用这个函数的线程暂停执行一段时间,不会创建新线程
}

void demo_all_tools() {
    std::cout << "=== 开始演示所有计时工具 ===" << std::endl;
    
    // 1. 演示基础 Timer
    {
        std::cout << "\n1. 基础 Timer 演示:" << std::endl;
        TimeUtils::Timer timer("基础任务");
        simulate_work(80);
        timer.print_elapsed("基础任务完成");
        std::cout << "基础任务: " << timer.elapsed_ms() << " ms" << std::endl;
    }
    
    // 2. 演示 ScopedTimer
    {
        std::cout << "\n2. ScopedTimer 演示:" << std::endl;
        TIME_SCOPE("外层作用域");
        
        simulate_work(30);
        
        {
            TIME_SCOPE("内层作用域1");
            simulate_work(20);
        }//离开作用域: 内层作用域1 - 总耗时: 20ms
        
        {
            TIME_SCOPE("内层作用域2");
            simulate_work(40);
        }//离开作用域: 内层作用域2 - 总耗时: 40ms
    }//离开作用域: 外层作用域 - 总耗时: 90ms
    
    // 3. 演示 PerformanceMonitor
    {
        std::cout << "\n3. PerformanceMonitor 演示:" << std::endl;
        TimeUtils::PerformanceMonitor monitor("测试任务");
        
        for (int i = 0; i < 5; i++) {
            TimeUtils::Timer iter_timer;
            simulate_work(20 + i * 5);
            monitor.record(iter_timer.elapsed_ms());//total_time_ += duration_ms;
        }
        
        monitor.print_stats();
    }
    
    // 4. 演示 PerformanceManager
    {
        std::cout << "\n4. PerformanceManager 演示:" << std::endl;
        
        // 记录到全局管理器
        for (int i = 0; i < 3; i++) {
            TimeUtils::Timer timer;
            simulate_work(25);
            TimeUtils::PerformanceManager::record("全局任务A", timer.elapsed_ms());
        }
        
        for (int i = 0; i < 4; i++) {
            TimeUtils::Timer timer;
            simulate_work(35);
            TimeUtils::PerformanceManager::record("全局任务B", timer.elapsed_ms());
        }
    }
}

void demo_timer_stamp() {
    std::cout << "\n=== 开始演示 TimerStamp ===" << std::endl;
    ClassA a;
    ClassB b;
    // 清空之前的时间戳（可选）
    TimeUtils::TimestampManager::clear();
    
    std::cout << "=== 开始跨类时间测量 ===" << std::endl;
    // 执行类A
    a.process();
    TimeUtils::TimestampManager::record("ClassA_start1");//时间戳带键名 好用
    uint64_t current = TimeUtils::TimestampManager::get_current_time();
    std::cout<< "current: " << current << std::endl;
    // TimeUtils::TimestampManager::record("ClassA_start");//时间戳带键名
    
    // 中间可以有一些其他操作
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
    
    // 执行类B
    b.process();
    // 最终的时间差报告
    std::cout << "\n=== 最终时间差报告 ===" << std::endl;
    try {
        TimeUtils::TimestampManager::print_difference("ClassA_start", "ClassB_start");
        TimeUtils::TimestampManager::print_difference("ClassA_end", "ClassB_start");
        TimeUtils::TimestampManager::print_difference("ClassA_start", "ClassB_end");
        TimeUtils::TimestampManager::print_difference("ClassA_end", "ClassB_end");
    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
    }
}

int main() {
    // 演示所有工具
    // demo_all_tools();
    demo_timer_stamp();
    
    // 打印全局性能统计
    std::cout << "\n=== 全局性能统计 ===" << std::endl;
    TimeUtils::PerformanceManager::print_all_stats();
    
    return 0;
}